# Kestrel: An XMPP-based Many-Task Computing Scheduler
# Author: Lance Stout <lancestout@gmail.com>
#
# Copyright 2009-2010 Clemson University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


class Worker(object):
    """
    Data structure representing a worker agent.

    A worker is an agent that has a worker role
    which allows it to execute tasks.
    """

    def __init__(self, data=None):
        """
        Instantiate a worker object given a profile.

        source (string):
          The name of the worker agent
        provides (list | string):
          A list of tags identifying the capabilities
          of the worker.
        """
        if data is None:
            data = {}

        self.id = data.get('source', '')
        self.provides = set(data.get('provides', []))
        self.status = 'offline'
        self.task = (None, None)

    def matches(self, job_tags):
        """
        Determine if the worker can accept a task from a job.
        """
        return self.provides.issuperset(job_tags)

    def __repr__(self):
        """Return the worker's ID."""
        return "(Worker: %s)" % self.id
